package top.fullj;

/**
 * @author bruce.wu
 * @since 2021/8/3 14:42
 */
public final class StopWatch {

    private transient long startTime;
    private transient long stopTime;

    public StopWatch() {
        this(false);
    }

    public StopWatch(boolean autoStart) {
        if (autoStart) {
            start();
        }
    }

    public StopWatch start() {
        this.startTime = System.currentTimeMillis();
        return this;
    }

    public void stop() {
        this.stopTime = System.currentTimeMillis();
    }

    public long getTime() {
        if (startTime == 0) {
            throw new IllegalStateException("invalid start time");
        }
        if (stopTime == 0) {
            throw new IllegalStateException("invalid stop time");
        }
        return stopTime - startTime;
    }

}
